package com.snopy.socket;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.*;

/**
 * @author snopy
 * @version 1.0.0
 * @classname Demo3
 * @description
 * @email 77912204@qq.com
 * @date 2022/06/10 15:43
 */
@Slf4j
public class SocketClient {
    public static void main(String[] args) throws IOException {
        try {
            ServerSocket serverSocket = new ServerSocket();
            Socket socket = new Socket();
        Thread thread = new ClientThread(socket);
        thread.start();
        }catch (Exception e){

        }
    }
}
class ClientThread extends Thread {
    private Socket socket;


    public ClientThread(Socket t_socket) {
        socket = t_socket;
    }

    @Override
    public void run() {
        System.out.println("线程开始，打印该客户端的ip和端口");
        try {
            socket.connect(new InetSocketAddress("127.0.0.1",80));
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            /*
            * HTTP 报文格式
            * 请求报文：
            * 第一行 起始行 用来说明要干什么
            * 第二行 起始行后面有0个或多个首部字段，每个首部字段都包含一个名字和一个值（"Content-Length: 11"）
            * */
            out.write("GET HTTP/1.2\r\n");
            out.write("Content-Type: charset\r\n");
            out.write("\r\n");

            out.flush();
            out.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}